<?php
class UsersController extends AppController {

    var $name = 'Users';
    //var $helpers = array('Ajax', 'Javascript', 'Html', 'Form');
    var $components = array('Session', 'Redirect','common','RequestHandler','Email');
    //var $paginate = array();
    function beforeFilter(){
        parent::beforeFilter();
    }
    
    
    
	function index() 
    {
        //$users = $this->User->Post->find("all");
		$this->User->recursive = 0;
		$this->set('users', $this->paginate());             
	}
 
	function view($id = null) 
    {
		if(!$id)
		{
			$this->Redirect->flashWarning('Th�ng tin th�nh vi�n kh�ng t?n t?i.', array('action' =>
				'index'));
		}
		$this->set('user', $this->User->read(null, $id));
	}

	function add() 
    {
   	    if(!empty($this->data))
		{
			$this->User->create();
			if($this->User->save($this->data))
			{
                $this->Redirect->flashSuccess('Th�m th�nh vi�n th�nh c�ng');
				if(!$this->RequestHandler->isAjax())
				{
					$this->redirect(array('action' => 'index'));
				}
				$this->set('closeModalbox', true);
			}
			else
			{
				$this->Redirect->flashWarning('Kh�ng th�m du?c th�ng tin, vui l�ng th? l?i.');
			}
		}
	}

	function edit($id = null)
	{

		if (!$id && empty($this->data))
		{
			$this->Redirect->flashWarning('Th�ng tin c?p nh?t kh�ng h?p l?.', array('action' =>'index'));
		}
		if (!empty($this->data))
		{
			if ($this->User->save($this->data))
			{
				$this->Redirect->flashSuccess('C?p nh?t th�ng tin th�nh c�ng');
                if (! $this->RequestHandler->isAjax()) 
				{
					$this->redirect(array('action' => 'index'));
				}
				$this->set('closeModalbox', true);
			} 
                        else
			{
				$this->Redirect->flashWarning('C?p nh?t th�ng tin th?t b?i.');
			}
		}
		if (empty($this->data))
		{
			$this->data = $this->User->read(null, $id);
		}
	}

	function delete($id = null) 
    {
		if(!$id)
		{
			$this->Redirect->flashWarning('Th�ng tin th�nh vi�n c?n x�a kh�ng t?n t?i.',
				array('action' => 'index'));
		}
		if($this->User->delete($id))
		{
			$this->Redirect->flashSuccess('X�a th�nh vi�n th�nh c�ng.', array('action' =>'index'));
		}
		$this->Redirect->flashWarning('X�a th�nh vi�n th?t b?i, vui l�ng th? l?i.',
			array('adction' => 'index'));
	}

   
    function login()
		{
		
		$user = $this->User->find('first', array('conditions'=>array('username'=>$this->data['User']['username'], 
                'password'=>md5($this->data['User']['password']))));
		}
		
        
    function logout()
        {
            $this->redirect($this->Auth->logout());
        }
        
    function doimk() 
		{
		  if(!empty($this->data)) {
          $a = $this->Session->read('cur_id');
          //$b = $this->Session->read('ho_ten');
          //echo $b;
          $user =$this->User->find('first', array('conditions'=>array('User.id'=>$a, 
                'password'=>md5($this->data['User']['old_password']))));
                 
		  if ($user)
                {
                //$a = md5($this->data['User']['new_password'])
				//$record['User']['password']= md5($this->data['User']['new_password']);
                //debug($record['User']['password']);
                //$record['User']['username']= $b;
				//$record['User']['id'] = $a;
                //echo $a;
                //echo $b;
                //debug($record);
                
                $this->User->id = $a ; 
                $pass = md5($this->data['User']['new_password']);
				if($this->User->saveField('password',$pass))
                //if $this->User->saveField('username',$a)
				{
					$this->Session->setFlash("�?i m?t kh?u th�nh c�ng.");
					$this->redirect('/users');
				}
                else $this->Session->setFlash('th?t b?i');
			}
          else $this->Session->setFlash("Mat khau cu ko dung");
		}
        }
        
        function __sendPasswordEmail($user, $password) {
                if ($user === false) {
                echo 'failed to retrieve User data for user.id: '.$user['User']['id'];
                return false;
                }
                $this->set('user', $user['User']);
                $this->set('password', $password);
                $this->Email->to = $user['User']['email']; //email nguoi dung
                $this->Email->subject = 'Password Change Request';// chu de cua email
                $this->Email->from = 'No-reply noreply@your-domain.com>'; //email cua nguoi goi
                return $this->Email->send('Your new password is: '.$password);
        }
        
        function createTempPassword() 
            {
	           $pass = rand(100000,999999);
	           return $pass;
	       }
        
        function forgot() {
	       if(!empty($this->data)) {
		  $user = $this->User->findByEmail($this->data['User']['email']);
		  if($user) {
		      $a = $this->Session->read('cur_id');
              //$this->User->id = $a ; 
              $record['User']['id'] = $a;
              //echo $a;
		      $newpass = $this->createTempPassword();
		      $record['User']['password'] = $this->Auth->password($newpass);
		      if($this->User->saveAll($record)) {
			     $this->__sendPasswordEmail($user, $newpass);
			     $this->Session->setFlash('A new password was sent to your email');
			     $this->redirect($this->referer());
		          }
		      } else {
		          $this->Session->setFlash('Email is not existed!');
		}
	  }
	}
        
  	
}
?>